﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.ComponentModel;

namespace Southridge.Data
{
    public partial class Profile : global::System.Data.Objects.DataClasses.EntityObject, IDataErrorInfo 
    {

        public Profile()
        {            
          //  this.Address = new Address(); 
        } 

        public static Profile FindFirst(string first, string last)
        {
            SouthridgeDB db = DBContext.SouthridgeDB;
            {
                try
                {

                    ObjectQuery<Profile> contactQuery = db.Profile
                        .Where("it.LastName = @ln AND it.FirstName = @fn",
                        new ObjectParameter("ln", last),
                        new ObjectParameter("fn", first));
                    System.Diagnostics.Debug.WriteLine(contactQuery.ToTraceString());

                    Profile p = contactQuery.First<Profile>();
                    return p;
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
            return null;

        }


        public void SimpleSave()
        {
            SouthridgeDB db = DBContext.SouthridgeDB;
            {
           //      db.AddObject("Profile", this); 
                 
                db.SaveChanges();                
            }



        }

        public static Profile FindById(int id)
        {
            SouthridgeDB db = DBContext.SouthridgeDB;
            {
                Profile p = FindById(id, db);
                return p;
            }
        }

        public static Profile FindById(int id, ObjectContext oc)
        {
            SouthridgeDB db = oc as SouthridgeDB;
            if (db != null)
            {
                try
                {
                    ObjectQuery<Profile> contactQuery = db.Profile
                     .Where("it.Id = @id ",
                     new ObjectParameter("id", id));
                    System.Diagnostics.Debug.WriteLine(contactQuery.ToTraceString());

                    Profile p = contactQuery.Include("Address").Include("Preference").First<Profile>();
                    // Profile p = contactQuery.First<Profile>();
                     
                     

                    return p;

                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.Message);
                }
            }
            return null;
        }

        public bool IsValid
        {
            get
            {
                if (Address != null)
                    return Address.IsValid;

                return false;
            }
        }

        protected bool _isDirty = false;
        public bool IsDirty
        {
            get
            {
                return _isDirty || Address.IsDirty;
            }
            internal set
            {
                if (_isDirty != value)
                {
                    _isDirty = value;
                    OfflinePropertyChanged("IsDirty");
                }
            }
        }

        protected void OfflinePropertyChanged(string name)
        {

           
        }

        protected override void OnPropertyChanged(string property)
        {
            base.OnPropertyChanged(property);
            IsDirty = true;
        }

        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get {
                    _errors.Remove(columnName);

                    switch (columnName)
                    {

                        case "FirstName":
                            {
                                if (this.FirstName == string.Empty)
                                {
                                    _errors.Add(columnName, Resources.FirstNameRequired);
                                    return Resources.FirstNameRequired;
                                }
                            }
                            break;
                        case "LastName":
                            {
                                if (this.LastName == string.Empty)
                                {
                                    _errors.Add(columnName, Resources.LastNameRequired);
                                    return Resources.LastNameRequired;
                                }
                            }
                            break;

                        case "HomePhoneNo":
                            {
                                if (this.HomePhoneNo == string.Empty)
                                {
                                    _errors.Add(columnName, Resources.HomePhoneNoRequired);
                                    return Resources.HomePhoneNoRequired;
                                }
                            }
                            break;
                    }


                    return "";
                }                    
        }
        private Dictionary<string, string> _errors = new Dictionary<string, string>();  

        #endregion
    }

}
